package edu.gdin.ecommerce.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import edu.gdin.ecommerce.dao.BaseDAO;
import edu.gdin.ecommerce.model.OperLog;
import edu.gdin.ecommerce.model.User;

public class OperLogUtil {

	public static void log(BaseDAO baseDAO, String type, String result, String attachNo, String operDesc) {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		OperLog log = new OperLog();
		log.setType(type);
		log.setResult(result);
		log.setOperTime(DateUtil.now());
		log.setAttachNo(attachNo);
		log.setOperDesc(operDesc);
		log.setOperIp(request.getRemoteAddr());
		if(request.getRemoteAddr().equals("0:0:0:0:0:0:0:1")) {
			log.setOperIp("localhost");
		}
		
		if(type.equals("1")) {
			log.setAccount(operDesc);
			log.setOperDesc("用户" + operDesc + "登录成功");
		} else {
			Map<String, Object> session = (Map<String, Object>) ActionContext.getContext().getSession();
			User user = (User) session.get("user");
			log.setAccount(user.getAccount());
		}
		
		baseDAO.insert(log);
	}
	
}
